from django.http import HttpResponse
import json
import requests
from bs4 import BeautifulSoup
import re

#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    lis = soup.select(".searchlist_content .highlightover")

    for li in lis:
        tempData = resultData()
        try:
            tempData["titleName"] = li.select("a")[0].text.strip()
        except:
            pass
        try:
            tempData["titleHref"] = 'http://222.29.253.12:8083/UI/ExamPaper/'+li.select("a")[0]["href"]
        except:
            pass
        try:
            tempData["source"] = li.select(".detaila")[0].text.strip();
        except:
            pass
        try:
            tempData["publishTime"] = li.select(".timea")[0].text.strip();
        except:
            pass
        try:
            tempData["category"] = li.select(".stylea")[0].text.strip();
        except:
            pass
        allData.append(tempData)

    return allData

def getData(keyword):
    headers = {
        'Proxy-Connection': 'keep-alive',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
        'Referer': 'http://222.29.253.12:8083/UI/css/pages.css',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'X-Requested-With': 'XMLHttpRequest',
        'Origin': 'http://222.29.253.12:8083',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    }

    params = (
        ('kclass', '-1'),
        ('keyword', keyword),
    )

    data = {
        'pageName': 'page_ExamPaperListNew.aspx',
        'id': ''
    }
    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    response = requests.post('http://222.29.253.12:8083/UI/ExamPaper/page_ExamPaperListNew.aspx', headers=headers,params=params, data=data,proxies=proxies)

    allData = formData(response)

    return allData


def index(request):

    data = json.loads(request.POST.get('data', '1'))

    allData= getData(data["values"][0])

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")